package com.story.modular.app.service.impl;

import com.story.config.properties.UpyunProperties;
import com.story.core.util.DateUtil;
import com.story.modular.app.service.UpyunService;
import main.java.com.UpYun;
import main.java.com.upyun.UpException;
import main.java.com.upyun.UpYunUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @author Administrator
 * @decription 又拍云管理
 * @Date 2017-12-27 13:23
 */
@Service
public class UpyunServiceImpl implements UpyunService {

    @Autowired
    private UpYun upYun;

    @Autowired
    private UpyunProperties properties;

    /**
     * 读取目录下的文件列表
     * @param path
     * @return
     * @throws IOException
     */
    @Override
    public List<UpYun.FolderItem> readDir(String path) throws IOException {
        List<UpYun.FolderItem> folderItemList = new ArrayList<UpYun.FolderItem>();
        try {
            folderItemList = upYun.readDir(path);
        } catch (UpException e) {
            e.printStackTrace();
            return folderItemList;
        }
        return folderItemList;
    }

    /**
     * 创建目录
     * @param path
     * @return
     * @throws IOException
     */
    @Override
    public String mkdir(String path) throws IOException {
        boolean result = false;
        try {
            result = upYun.mkDir(path);
        } catch (UpException e) {
            e.printStackTrace();
            return "";
        }
        if(result) {
            return path;
        } else {
            return "";
        }
    }

    /**
     * 删除目录
     * @param path
     * @return
     * @throws IOException
     */
    @Override
    public boolean rmDir(String path) throws IOException {
        try {
            return upYun.rmDir(path);
        } catch (UpException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 上传纯文本
     * @param datas
     * @return
     * @throws IOException
     * @throws UpException
     */
    @Override
    public String uploadFile(String datas) throws IOException, UpException {
        String filePath = properties.getImagePath() + "/" + DateUtil.getDay() + "/" + UUID.randomUUID();
        boolean result = upYun.writeFile(filePath,datas,true);
        if(result) {
            return properties.getDomain() + "/" + filePath;
        } else {
            return "";
        }
    }

    /**
     * 上传本地文件
     * @param localFilePath
     * @param fileType
     * @return
     * @throws IOException
     * @throws UpException
     */
    @Override
    public String uploadFile(String localFilePath, String fileType) throws IOException, UpException {
        String filePath = properties.getImagePath() + "/" + DateUtil.getDays() + "/" + UUID.randomUUID() + fileType;
        File file = new File(localFilePath);
        upYun.setContentMD5(UpYun.md5(file));
        boolean result = upYun.writeFile(filePath,file,true);
        if(result) {
            return properties.getDomain() + "/" + filePath;
        }
        return "";
    }

    /**
     * 上传本地文件
     * @param file
     * @param fileType
     * @return
     * @throws IOException
     * @throws UpException
     */
    @Override
    public String uploadFile(File file, String fileType) throws IOException, UpException {
        String filePath = properties.getImagePath() + "/" + DateUtil.getDays() + "/" + UUID.randomUUID() + fileType;
        upYun.setContentMD5(UpYun.md5(file));
        boolean result = upYun.writeFile(filePath,file,true);
        if(result) {
            return properties.getDomain() + "/" + filePath;
        }
        return "";
    }

    /**
     *
     * @param inputStream
     * @param fileType
     * @return
     * @throws IOException
     * @throws UpException
     */
    @Override
    public String uploadFile(InputStream inputStream,String fileType) throws IOException, UpException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while ((rc = inputStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] fileByte = swapStream.toByteArray();
        String filePath = properties.getImagePath() + "/" + DateUtil.getDays() + "/" + UUID.randomUUID() + fileType;
        boolean result = upYun.writeFile(filePath,fileByte,true);
        if(result) {
            return properties.getDomain() + "/" + filePath;
        }
        return "";
    }

    /**
     * 获取文件信息
     * @param filePath
     * @return
     * @throws IOException
     * @throws UpException
     */
    @Override
    public Map<String, String> getFileInfo(String filePath) throws IOException, UpException {
        return upYun.getFileInfo(filePath);
    }

    /**
     * 获得整体使用量
     * @return
     */
    @Override
    public long getUsage() throws IOException, UpException {
        return upYun.getBucketUsage();
    }

    /**
     * 获得某一个目录的使用量
     * @return
     */
    @Override
    public long getFolderUsage(String path) throws IOException, UpException {
        return upYun.getFolderUsage(path);
    }

    /**
     * 得到防盗链token
     * @param path
     * @return
     * @throws IOException
     * @throws UpException
     */
    @Override
    public String getTokenSign(String path,long expireTime) {
        Long nowTimeStp = DateUtil.format(new Date()).getTime() / 1000;
        Long eTime = nowTimeStp + expireTime;
        String token = UpYunUtils.md5(properties.getTokenSecretKey()+"&"+eTime+"&"+path).substring(12,20)+eTime;
        return token;
    }

}
